package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.DepartmentService;
import service.PositionDescriptionService;
import entityJPA.Candidate;
import entityJPA.Department;
import entityJPA.Position;
import entityJPA.Positiondescription;
import entityJPA.Positionstatus;

/**
 * Servlet implementation class AddPositionDescriptionServlet
 */
@WebServlet("/AddPositionDescriptionServlet")
public class AddPositionDescriptionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	PositionDescriptionService posDescService;
	@EJB
	DepartmentService dService;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddPositionDescriptionServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		List<Department> dep = dService.getAllDepartments();
		HttpSession h= request.getSession();
		h.setAttribute("dep", dep);
		response.sendRedirect("addPositionDescription.jsp");
		if (request.getParameter("insert") != null) {
			add(request, response);
		} else if (request.getParameter("update") != null) {
			try {
				edit(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}}
	}

	public int add(HttpServletRequest request, HttpServletResponse response) {
		Positiondescription posd = new Positiondescription();
		String requirements = request.getParameter("requirements");
		String responsabilities = request.getParameter("responsabilities");
		String project = request.getParameter("project");
		int id = Integer.parseInt(request.getParameter("department"));
		Department depa = dService.findDepartmentById(id);
		posd.setDepartment(depa);
		posd.setProject(project);
		posd.setRequirements(requirements);
		posd.setResponsabilities(responsabilities);
		posDescService.insert(posd);
		return posDescService.findPositiondescriptionByValues(posd);
	}

	public int edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int id = Integer.parseInt(request.getParameter("idDescription"));
		String requirements = request.getParameter("requirements");
		String responsabilities = request.getParameter("responsabilities");
		String project = request.getParameter("project");
		Positiondescription posd = posDescService.findPositiondescriptionById(id);
		int idd = Integer.parseInt(request.getParameter("department"));
		Department dep = dService.findDepartmentById(idd);
		posd.setProject(project);
		posd.setRequirements(requirements);
		posd.setResponsabilities(responsabilities);
		posd.setDepartment(dep);
		posDescService.edit(posd);
		return posDescService.findPositiondescriptionByValues(posd);
	}

}
